package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.Department;
import cn.itsource.ymcc.mapper.DepartmentMapper;
import cn.itsource.ymcc.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zongchangjiang
 * @since 2023-03-26
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Override
    public List<Department> getTree() {
        //先要查询所有
        List<Department> list = super.list();
        List<Department> first = new ArrayList<>();
        for (Department department : list) {
            if(department.getParentId() == null){//就是顶级
                first.add(department);
            }else {
                //找爹
                for (Department dept : list) {
                    if(department.getParentId().equals(dept.getId())){
                        dept.getChildren().add(department);
                    }
                }
            }
        }
        return first;
    }
}








